package com.yuan.base.framework.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.yuan.base.framework.BaseCommonInterface;
import com.yuan.base.utils.LogUtil;

import org.xutils.common.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;


/**
 */
public class BaseHttpCallBack<T> {
	private String TAG = "BaseHttpCallBack";

	private Class<T> entityClass;

	private Class<T> subEntityClass;

	private Callback.Cancelable cancelable;

	public Callback.Cancelable getCancelable() {
		return cancelable;
	}

	public void setCancelable(Callback.Cancelable cancelable) {
		this.cancelable = cancelable;
	}

	protected BaseHttpCallBack() {
		// 获取泛型类型
		Type type = getClass().getGenericSuperclass();
		Type[] trueType = ((ParameterizedType) type).getActualTypeArguments();
		if (trueType[0] instanceof ParameterizedType) {
			this.entityClass = (Class<T>) ((ParameterizedType) trueType[0]).getRawType();
			Type[] trueType2 = ((ParameterizedType) trueType[0]).getActualTypeArguments();
			if (trueType2[0] instanceof ParameterizedType)
				this.subEntityClass = (Class<T>) ((ParameterizedType) trueType2[0]).getRawType();
			else
				this.subEntityClass = (Class<T>) trueType2[0];
		} else {
			this.entityClass = (Class<T>) trueType[0];
		}
	}

	void handleEntry(String result) {
		LogUtil.i(TAG, "entityClass===>" + entityClass);
		LogUtil.i(TAG, "subEntityClass===>" + subEntityClass);
//		LogUtil.i(TAG, "Http Result===>" + result);
		T o;
		try {
			if (entityClass.equals(String.class)) {
				o = (T) result;
			} else if (entityClass.equals(List.class)) {
				o = (T) JSON.parseArray(result, subEntityClass);
			} else {
				o = JSON.parseObject(result, entityClass);
			}
		} catch (JSONException e) {
			LogUtil.e(TAG, "数据解析错误:" + BaseErrorCode.JSON_PARSE_ERROR + ",返回值:" + result.toString());
			onFailure(BaseErrorCode.JSON_PARSE_ERROR, BaseErrorCode.errorMsg.get(BaseErrorCode.JSON_PARSE_ERROR, "内部错误,请将错误反馈给我们！"));
			return;
		}
		try {
			onSuccess(o);
		} catch (Exception e) {
			e.printStackTrace();
			LogUtil.e(TAG, "回调处理错误");
			onFailure(BaseErrorCode.APP_HANDLE_ERROR, BaseErrorCode.errorMsg.get(BaseErrorCode.APP_HANDLE_ERROR));
		}
	}

	/**
	 * 请求完成的回调方法
	 * @param json 请求回的原始数据
	 * @return 返回true会继续调用success
	 */
	public boolean onComplete(String json) {
		return true;
	}

	/**
	 * onPreStart网络访问开始
	 */
	public void onPreStart() {
	}

	/**
	 * onPost网络访问后
	 */
	public void onPost() {
	}

	public void onLoading(long current, long total) {
	}


	/**
	 * onFailure 失败 回调方法
	 * @param code 参考ErrorCode.java，如果是服务器内部错误，返回http的错误码，一般为5xx
	 * @param msg  错误内容，如果是服务器内部错误，返回空字符串。
	 */
	public void onFailure(int code, String msg) {
		if (BaseCommonInterface.getHttpCallBackHandle() != null) {
			BaseCommonInterface.getHttpCallBackHandle().onDefaultFailure(code, msg);
		}
	}

	/**
	 * onSuccess 成功回调方法
	 * @param result 只支持String，pojo，List<POJO> ，不支持其他集合类型
	 */
	public void onSuccess(T result) {
	}

}
